package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysEarnings;
import com.ruoyi.project.system.domain.SysQueryPay;
import com.ruoyi.project.system.mapper.SysQueryPayMapper;
import com.ruoyi.project.system.service.ISysEarningsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 收益Controller
 * 
 * @author ruoyi
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/earnings")
public class SysEarningsController extends BaseController
{
    @Autowired
    private ISysEarningsService sysEarningsService;


    @Autowired
    private SysQueryPayMapper sysQueryPayMapper;

    /**
     * 用户摇红包
     */
    @GetMapping("/usrp")
    public AjaxResult userShakeRedPackage() {
        Long userId = SecurityUtils.getUserId();
        // 拉取一单可用红包支付单
        SysQueryPay redPacketOrder = sysQueryPayMapper.redPacketOrder(userId);
        AjaxResult result = sysEarningsService.shakeRedPackage(redPacketOrder);
        // 计算剩余次数
        result.put("rpc", sysQueryPayMapper.redPacketCount(userId));
        // 返回数据
        return result;
    }

    /**
     * 查询收益列表
     */
    @PreAuthorize("@ss.hasPermi('system:earnings:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysEarnings sysEarnings)
    {
        startPage();
        List<SysEarnings> list = sysEarningsService.selectSysEarningsList(sysEarnings);
        return getDataTable(list);
    }

    /**
     * 导出收益列表
     */
    @PreAuthorize("@ss.hasPermi('system:earnings:export')")
    @Log(title = "收益", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysEarnings sysEarnings)
    {
        List<SysEarnings> list = sysEarningsService.selectSysEarningsList(sysEarnings);
        ExcelUtil<SysEarnings> util = new ExcelUtil<>(SysEarnings.class);
        util.exportExcel(response, list, "收益数据");
    }

    /**
     * 获取收益详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:earnings:query')")
    @GetMapping(value = "/{earningsId}")
    public AjaxResult getInfo(@PathVariable("earningsId") Long earningsId)
    {
        return success(sysEarningsService.selectSysEarningsByEarningsId(earningsId));
    }

    /**
     * 新增收益
     */
    @PreAuthorize("@ss.hasPermi('system:earnings:add')")
    @Log(title = "收益", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysEarnings sysEarnings)
    {
        return toAjax(sysEarningsService.insertSysEarnings(sysEarnings));
    }

    /**
     * 修改收益
     */
    @PreAuthorize("@ss.hasPermi('system:earnings:edit')")
    @Log(title = "收益", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysEarnings sysEarnings)
    {
        return toAjax(sysEarningsService.updateSysEarnings(sysEarnings));
    }

    /**
     * 删除收益
     */
    @PreAuthorize("@ss.hasPermi('system:earnings:remove')")
    @Log(title = "收益", businessType = BusinessType.DELETE)
	@DeleteMapping("/{earningsIds}")
    public AjaxResult remove(@PathVariable Long[] earningsIds)
    {
        return toAjax(sysEarningsService.deleteSysEarningsByEarningsIds(earningsIds));
    }
}
